"""
主题: 带可选参数的装饰器
问题: 你想写一个装饰器，既可以不传参数给它，也可以传递可选参数给它
提示 : 
"""

from functools import wraps, partial
import logging


def logged(func=None, *, level=logging.DEBUG, name=None, message=None):
    if func is None:
        return partial(logged, level=level, name=name, message=message)

    logname = name if name else func.__module__
    log = logging.getLogger(logname)
    logmsg = message if message else func.__name__

    @wraps(func)
    def wrapper(*args, **kwargs):
        log.log(level, logmsg)
        return func(*args, **kwargs)

    return wrapper


# Example use
@logged
def add(x, y):
    return x + y


@logged(level=logging.CRITICAL, name='example')
def spam():
    print('Spam!')

def aa(kk=None, *, a=1,b=2,c=3):
    print(kk, a, b, c)

def recipe1():
    print(f"{add(1, 2) = }")
    print(f"{spam() = }")  

def recipe2():
    bbb = partial(aa, a=4,b=5)
    bbb('333')  

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()

if __name__ == '__main__':
    main()      